﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Web.Mvc;
using RedSand.Site.Models;

namespace RedSand.Site.Controllers
{
    public class CustomPcController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Manager()
        {
            return View(new ManagerModel { Title = "Custom Pc manager", AttachTinyMce = true });
        }
        [AjaxOrChildAction]
        public ActionResult List(ProductSearchModel searchModel, int page = 1)
        {
            int Total = 0;
            ViewBag.RouteValues = searchModel;
            var list = RsDataManager.GetList<CustomPcInfo>("CustomPcGetAll");
            ViewBag.TotalRecord = Total;
            return PartialView(list);
        }
        [AjaxOrChildAction, HttpGet]
        public ActionResult Create()
        {
            return PartialView();
        }
        [AjaxOrChildAction, HttpPost]
        public ActionResult Create(CustomPcInfo info, string NewImage)
        {
            if (string.IsNullOrWhiteSpace(info.Details))
            {
                return AjaxMessage(Message.ContentEmpty);
            }
            if (string.IsNullOrWhiteSpace(NewImage))
            {
                return AjaxMessage("Please choose a image");
            }
            if (ModelState.IsValid)
            {
                info.Details = HtmlDecode(info.Details);
                if (!string.IsNullOrWhiteSpace(NewImage)) info.Image = SaveImage(null);
                int result = RsDataManager.Save("CustomPcSave", info);
                if (result > 0)
                {
                    AddMessage("Create success, please custom include for this pc");
                    return RedirectToAction("Edit", new { id = result, create = true });
                }
                else if (result == -2)
                {
                    DeleteFile(info.Image);
                    return AjaxMessage("please choose another Alias");
                }
                else
                {
                    DeleteFile(info.Image);
                    return AjaxMessage(Message.CreateError);
                }
            }
            return AjaxModelError();
        }
        [AjaxOrChildAction, HttpGet]
        public ActionResult Edit(int id, bool create = false)
        {
            ViewBag.Create = create;
            return PartialView(RsDataManager.GetInfo<CustomPcInfo>("CustomPcGetInfo", "CustomPcID", id));
        }
        [AjaxOrChildAction, HttpPost]
        public ActionResult Edit(CustomPcInfo info, string NewImage)
        {
            if (string.IsNullOrWhiteSpace(info.Details))
            {
                return AjaxMessage(Message.ContentEmpty);
            }
            if (ModelState.IsValid)
            {
                info.Details = HtmlDecode(info.Details);
                if (!string.IsNullOrWhiteSpace(NewImage)) info.Image = SaveImage(null);
                int result = RsDataManager.ExecuteGetReturn("CustomPcSave", info);
                if (result > 0)
                {
                    AddMessage(Message.UpdateSuccess);
                    return RedirectToAction("List");
                }
                else if (result == -2)
                {
                    if (!string.IsNullOrWhiteSpace(NewImage)) DeleteFile(info.Image);
                    return AjaxMessage("please choose another Alias");
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(NewImage)) DeleteFile(info.Image);
                    return AjaxMessage(Message.UpdateError);
                }
            }
            return AjaxModelError();
        }
        [AjaxOrChildAction]
        public ActionResult Delete(int[] id)
        {
            if (id != null)
            {
                int count = id.Count();
                foreach (int i in id)
                {
                    CustomPcInfo info = RsDataManager.GetInfo<CustomPcInfo>("CustomPcGetInfo", "CustomPcID", i);
                    if (RsDataManager.Delete("ProductDelete", "ProductID", i))
                    {
                        DeleteFile(info.Image);
                        count--;
                    }
                }
                if (count > 0)
                {
                    AddMessage("Cannot delete " + count + " records");
                }
                else
                {
                    AddMessage(Message.DeleteSuccess);
                }
            }
            return RedirectToAction("List");
        }


        [AjaxOrChildAction, HttpGet]
        public ActionResult Components(int id, int parent = 0)
        {
            ViewBag.ProductId = id;
            return PartialView(parent == 0 ? "Components" : "SubComponents", RsDataManager.GetList<ComponentsInfo>("ComponentsGetList", new { ParentID = parent }));
        }

        [AjaxOrChildAction, HttpPost]
        public ActionResult Components(int id, int[] ComponentsID, int[] ProductID)
        {
            for (int i = 0; i < ComponentsID.Length; i++)
            {
                RsDataManager.Save("IncludeSave", new { CustomPcID = id, ComponentsID = ComponentsID[i], ProductID = ProductID[i] });
            }
            return Json(true);
        }

        [AjaxOrChildAction]
        public MvcHtmlString ProductList(int category, int components, int custompc, string name, string id, int width=490)
        {
            var list = RsDataManager.GetList<ProductInfo>("ProductGetListForCustomPc", new { CategoryID = category }) ?? new List<ProductInfo>();
            int selectedValue = RsDataManager.ExecuteGetReturn("IncludeGetProductID", new { CustomPcID = custompc, ComponentsID = components });
            TagBuilder select = new TagBuilder("select");
            select.Attributes["name"] = name ?? "ProductID";
            select.GenerateId(id ?? "ProductID");
            select.Attributes["style"] = "width:" + width + "px";
            TagBuilder option = new TagBuilder("option");
            option.Attributes["value"] = "0-0";
            option.InnerHtml = "Not include";
            select.InnerHtml += option.ToString();
            foreach (var item in list)
            {
                option = new TagBuilder("option");
                option.Attributes["value"] = item.ProductID + "-" + item.Price;
                option.InnerHtml = item.Name;
                if (item.ProductID == selectedValue)
                {
                    option.Attributes["selected"] = "selected";
                }
                select.InnerHtml += option.ToString();
            }
            return MvcHtmlString.Create(select.ToString());
        }

    }
}
